<template>
  <PageSafeContainer>
    <view class="container">
      <view class="logo-img">
        <view class="logoimage">
          <image src="@/static/image/logo@2x.png" mode=""></image>
        </view>
        <text class="pw">陪玩</text>
      </view>
      <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="login-but">
        <view class="">
          <view class="">微信一键登录</view>
        </view>
      </button>
      <view class="agreement-check">
        <view class="">
          <u-checkbox-group>
            <u-checkbox v-model="checked" shape="circle" size="30rpx" activeColor="#ED79BE"></u-checkbox>
          </u-checkbox-group>
        </view>
        <view class="agreement-text">
          <text>若手机号未注册将进入注册流程，注册即为同意</text>
          <text class="agreement-user">《用户服务协议》</text>
        </view>
      </view>
    </view>
  </PageSafeContainer>
</template>

<script>
  import PageSafeContainer from '@/components/PageSafeContainer/PageSafeContainer.vue';
  export default {
    name: 'authorizationPhone',
    components: {
      PageSafeContainer
    },
    data() {
      return {
        checked: false,
      };
    },
    async onLoad() {

    },
    onShow() {},
    methods: {
      // 点击微信授权登录
      getPhoneNumber(e) {
        // return false;

        // if (this.pitchOn == 0) {
        // 	this.$.toast('请先阅读并同意协议');
        // 	return false;
        // }

        if (e.detail.errMsg == "getPhoneNumber:ok") {

          let that = this;
          uni.login({
            provider: 'weixin',
            success: function(loginRes) {
              // 获取用户信息				
              console.log(loginRes, 'login授权code');
              that.$.ajax("POST", "/api/login/weChatLoginTry", {
                code: loginRes.code,
                ajax_type: 'form'
              }, (res) => {
                if (res.code == 1000) {
                  if (res.data.status == 2) {
                    // 2:微信未绑定手机
                    return false
                  } else if (res.data.status == 1) { //登录结果 1:成功 2:微信未绑定手机
                    that.$.show();
                    that.$.set_data("token", res.data.token);
                    that.$.set_data("userForm", res.data.user);
                    that.$.open_tab('/pages/index/index');
                    that.$.hide();
                  }
                } else if (res.code == 8001) { //没有授权
                  console.log(e.detail.code, '手机号授权code')
                  // -----------------------
                  that.$.ajax("POST", "/api/login/wxLoginCreateUserCode", {
                    code: loginRes.code, //login授权code
                    phoneCode: e.detail.code, //手机号授权code
                    ajax_type: 'form'
                  }, (res1) => {
                    if (res1.code == 1000) {
                      // that.$.show();
                      that.$.set_data("token", res.data.token);
                      that.$.set_data("userForm", res.data.user);
                      that.$.open_tab('/pages/index/index');
                      // that.$.hide();
                    } else {
                      that.$.toast(res.message);
                    }
                  });
                } else {
                  that.$.toast(res.message);
                }
              });
            }
          });
        } else {
          //拒绝授权
          this.$.toast('已拒绝授权，无法登录');
        }

      }
    }
  }
</script>

<style lang="scss" scoped>
  page {
    background: #FFFFFF !important;
  }

  .container {
    padding: 0 20rpx;
  }

  .login-but {
    font-size: 30rpx;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    color: #FFFFFF;
    background: #05C160;
    border-radius: 42rpx;
  }

  .agreement-check {
    margin-top: 32rpx;
    display: flex;
  }

  .agreement-text {
    font-size: 26rpx;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    color: #999999;

    .agreement-user {
      font-size: 26rpx;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      color: #ED79BE;
    }
  }

  .logo-img {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    margin-top: 80rpx;
    margin-bottom: 50rpx;

    .logoimage {
      width: 160rpx;
      height: 160rpx;

      image {
        width: 100%;
        height: 100%;
      }
    }

    .pw {
      font-size: 32rpx;
      font-family: SourceHanSansSC, SourceHanSansSC;
      font-weight: bold;
      color: #111111;
      margin-top: 20rpx;
    }
  }
</style>